#import "PersonalCenterViewController.h"

@interface PersonalCenterViewController ()

@end

@implementation PersonalCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"个人中心";
    
    // 设置与 ViewController 一致的渐变背景
    CAGradientLayer *backgroundGradient = [CAGradientLayer layer];
    backgroundGradient.colors = @[(__bridge id)[UIColor colorWithRed:0.96 green:0.98 blue:1.0 alpha:1.0].CGColor,
                                  (__bridge id)[UIColor colorWithRed:0.94 green:0.96 blue:1.0 alpha:1.0].CGColor];
    backgroundGradient.frame = self.view.bounds;
    [self.view.layer insertSublayer:backgroundGradient atIndex:0];
    
    // 设置导航栏样式
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.26 green:0.54 blue:0.96 alpha:1.0];
    self.navigationController.navigationBar.titleTextAttributes = @{
        NSForegroundColorAttributeName: [UIColor blackColor],
        NSFontAttributeName: [UIFont systemFontOfSize:20 weight:UIFontWeightMedium]
    };
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
    // 添加返回按钮
    UIImage *backImage = [UIImage imageNamed:@"back"];
    if (backImage) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
    } else {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
    }
    
    [self setupUI];
}

- (void)setupUI {
    CGFloat width = self.view.bounds.size.width;
    CGFloat safeAreaTop = self.view.safeAreaInsets.top;
    
    // 个人信息卡片
    UIView *profileCard = [[UIView alloc] initWithFrame:CGRectMake(20, safeAreaTop + 120, width - 40, 160)];
    profileCard.backgroundColor = [UIColor whiteColor];
    profileCard.layer.cornerRadius = 24;
    profileCard.layer.shadowColor = [UIColor colorWithRed:0.26 green:0.54 blue:0.96 alpha:0.15].CGColor;
    profileCard.layer.shadowOpacity = 1;
    profileCard.layer.shadowOffset = CGSizeMake(0, 8);
    profileCard.layer.shadowRadius = 20;
    [self.view addSubview:profileCard];
    
    // 头像
    UIView *avatarContainer = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 80, 80)];
    avatarContainer.backgroundColor = [UIColor colorWithRed:0.26 green:0.54 blue:0.96 alpha:0.1];
    avatarContainer.layer.cornerRadius = 40;
    [profileCard addSubview:avatarContainer];
    
    UILabel *avatarIcon = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    avatarIcon.text = @"👤";
    avatarIcon.font = [UIFont systemFontOfSize:40];
    avatarIcon.textAlignment = NSTextAlignmentCenter;
    [avatarContainer addSubview:avatarIcon];
    
    // 用户信息
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 35, profileCard.bounds.size.width - 150, 28)];
    nameLabel.text = @"  ";
    nameLabel.font = [UIFont systemFontOfSize:24 weight:UIFontWeightBold];
    nameLabel.textColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
    [profileCard addSubview:nameLabel];
    
    UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 70, profileCard.bounds.size.width - 150, 20)];
    descLabel.text = @"老人记忆助手用户";
    descLabel.font = [UIFont systemFontOfSize:16];
    descLabel.textColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
    [profileCard addSubview:descLabel];
    
    // 版本信息
    UILabel *versionLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 100, profileCard.bounds.size.width - 150, 18)];
    versionLabel.text = @"版本 1.0.0";
    versionLabel.font = [UIFont systemFontOfSize:14];
    versionLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
    [profileCard addSubview:versionLabel];
    
    // 功能列表区域
    CGFloat listY = CGRectGetMaxY(profileCard.frame) + 30;
    UIView *listCard = [[UIView alloc] initWithFrame:CGRectMake(20, listY, width - 40, 160)];
    listCard.backgroundColor = [UIColor whiteColor];
    listCard.layer.cornerRadius = 20;
    listCard.layer.shadowColor = [UIColor colorWithRed:0.26 green:0.54 blue:0.96 alpha:0.1].CGColor;
    listCard.layer.shadowOpacity = 1;
    listCard.layer.shadowOffset = CGSizeMake(0, 6);
    listCard.layer.shadowRadius = 15;
    [self.view addSubview:listCard];
    
    // 隐私协议按钮
    UIButton *privacyButton = [self createMenuButtonWithTitle:@"隐私协议" 
                                                         icon:@""
                                                        frame:CGRectMake(0, 20, listCard.bounds.size.width, 60)
                                                       action:@selector(showPrivacyPolicy)];
    [listCard addSubview:privacyButton];
    
    // 分割线
    UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(20, 80, listCard.bounds.size.width - 40, 1)];
    separator.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
    [listCard addSubview:separator];
    
    // 服务协议按钮
    UIButton *serviceButton = [self createMenuButtonWithTitle:@"服务协议" 
                                                          icon:@""
                                                         frame:CGRectMake(0, 80, listCard.bounds.size.width, 60)
                                                        action:@selector(showServiceAgreement)];
    [listCard addSubview:serviceButton];
    
    // 底部装饰
    UIView *decorationView = [[UIView alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(listCard.frame) + 40, width - 40, 60)];
    decorationView.backgroundColor = [UIColor colorWithRed:0.96 green:0.98 blue:1.0 alpha:0.8];
    decorationView.layer.cornerRadius = 16;
    [self.view addSubview:decorationView];
    
    UILabel *decorationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, decorationView.bounds.size.width, 60)];
    decorationLabel.text = @" ";
    decorationLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    decorationLabel.textColor = [UIColor colorWithRed:0.26 green:0.54 blue:0.96 alpha:0.8];
    decorationLabel.textAlignment = NSTextAlignmentCenter;
    [decorationView addSubview:decorationLabel];
}

- (UIButton *)createMenuButtonWithTitle:(NSString *)title 
                                   icon:(NSString *)icon 
                                  frame:(CGRect)frame 
                                 action:(SEL)action {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    button.backgroundColor = [UIColor clearColor];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    // 图标
    UILabel *iconLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 15, 30, 30)];
    iconLabel.text = icon;
    iconLabel.font = [UIFont systemFontOfSize:24];
    iconLabel.textAlignment = NSTextAlignmentCenter;
    [button addSubview:iconLabel];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, frame.size.width - 140, 20)];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
    titleLabel.textColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
    [button addSubview:titleLabel];
    
    // 箭头
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width - 50, 20, 8, 12)];
    image.image = [UIImage imageNamed:@"next"];
    [button addSubview:image];
    
    // 添加点击效果
    [button addTarget:self action:@selector(buttonTouchDown:) forControlEvents:UIControlEventTouchDown];
    [button addTarget:self action:@selector(buttonTouchUp:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside | UIControlEventTouchCancel];
    
    return button;
}

#pragma mark - Button Actions

- (void)buttonTouchDown:(UIButton *)button {
    [UIView animateWithDuration:0.1 animations:^{
        button.backgroundColor = [UIColor colorWithRed:0.96 green:0.98 blue:1.0 alpha:1.0];
    }];
}

- (void)buttonTouchUp:(UIButton *)button {
    [UIView animateWithDuration:0.1 animations:^{
        button.backgroundColor = [UIColor clearColor];
    }];
}

- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)showPrivacyPolicy {
    [self showAgreementWithTitle:@"隐私协议" content:[self getPrivacyPolicyContent]];
}

- (void)showServiceAgreement {
    [self showAgreementWithTitle:@"服务协议" content:[self getServiceAgreementContent]];
}

- (void)showAgreementWithTitle:(NSString *)title content:(NSString *)content {
    UIViewController *agreementVC = [[UIViewController alloc] init];
    agreementVC.title = title;
    
    // 设置背景
    agreementVC.view.backgroundColor = [UIColor colorWithRed:0.96 green:0.98 blue:1.0 alpha:1.0];
    
    // 创建滚动视图
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:agreementVC.view.bounds];
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.showsVerticalScrollIndicator = YES;
    [agreementVC.view addSubview:scrollView];
    
    // 内容容器
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, scrollView.bounds.size.width - 40, 0)];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.layer.cornerRadius = 16;
    contentView.layer.shadowColor = [UIColor colorWithRed:0.26 green:0.54 blue:0.96 alpha:0.1].CGColor;
    contentView.layer.shadowOpacity = 1;
    contentView.layer.shadowOffset = CGSizeMake(0, 4);
    contentView.layer.shadowRadius = 10;
    [scrollView addSubview:contentView];
    
    // 内容文本
    UILabel *contentLabel = [[UILabel alloc] init];
    contentLabel.text = content;
    contentLabel.font = [UIFont systemFontOfSize:16];
    contentLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    contentLabel.numberOfLines = 0;
    contentLabel.lineBreakMode = NSLineBreakByWordWrapping;
    
    // 计算文本大小
    CGSize textSize = [content boundingRectWithSize:CGSizeMake(contentView.bounds.size.width - 40, CGFLOAT_MAX)
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                         attributes:@{NSFontAttributeName: contentLabel.font}
                                            context:nil].size;
    
    contentLabel.frame = CGRectMake(20, 20, contentView.bounds.size.width - 40, textSize.height);
    [contentView addSubview:contentLabel];
    
    // 更新容器高度
    CGRect contentFrame = contentView.frame;
    contentFrame.size.height = textSize.height + 40;
    contentView.frame = contentFrame;
    
    // 设置滚动视图内容大小
    scrollView.contentSize = CGSizeMake(scrollView.bounds.size.width, contentFrame.size.height + 40);
    
    // 添加关闭按钮
    agreementVC.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(dismissAgreement)];
    
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:agreementVC];
    navController.navigationBar.barTintColor = [UIColor colorWithRed:0.26 green:0.54 blue:0.96 alpha:1.0];
    navController.navigationBar.titleTextAttributes = @{
        NSForegroundColorAttributeName: [UIColor blackColor],
        NSFontAttributeName: [UIFont systemFontOfSize:20 weight:UIFontWeightMedium]
    };
    navController.navigationBar.tintColor = [UIColor blackColor];
    
    [self presentViewController:navController animated:YES completion:nil];
}

- (void)dismissAgreement {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSString *)getPrivacyPolicyContent {
    return @"隐私协议\n\n"
           @"感谢您使用老人记忆助手应用。我们非常重视您的隐私保护。\n\n"
           @"1. 信息收集\n"
           @"我们仅收集您主动提供的信息，包括：\n"
           @"• 您录入的物品信息和位置记录\n"
           @"• 您设置的提醒事项\n"
           @"• 您拍摄的照片和录制的语音\n\n"
           @"2. 信息使用\n"
           @"我们收集的信息仅用于：\n"
           @"• 为您提供记忆辅助服务\n"
           @"• 改善应用功能和用户体验\n"
           @"• 确保应用正常运行\n\n"
           @"3. 信息保护\n"
           @"• 您的所有数据都存储在本地设备上\n"
           @"• 我们不会将您的个人信息传输到外部服务器\n"
           @"• 我们采用适当的技术措施保护您的信息安全\n\n"
           @"4. 信息分享\n"
           @"我们不会向第三方分享、出售或披露您的个人信息，除非：\n"
           @"• 获得您的明确同意\n"
           @"• 法律法规要求\n\n"
           @"5. 您的权利\n"
           @"您有权：\n"
           @"• 随时查看、修改或删除您的信息\n"
           @"• 停止使用我们的服务\n"
           @"• 联系我们处理隐私相关问题\n\n"
           @"如有任何疑问，请联系我们。\n\n"
           @"最后更新时间：2024年1月";
}

- (NSString *)getServiceAgreementContent {
    return @"服务协议\n\n"
           @"欢迎使用老人记忆助手应用。请仔细阅读本服务协议。\n\n"
           @"1. 服务内容\n"
           @"老人记忆助手为您提供：\n"
           @"• 物品位置记录和查找功能\n"
           @"• 重要事项提醒功能\n"
           @"• 照片和语音记录功能\n"
           @"• 其他记忆辅助功能\n\n"
           @"2. 使用规则\n"
           @"您在使用本应用时应当：\n"
           @"• 遵守相关法律法规\n"
           @"• 不得用于非法用途\n"
           @"• 不得干扰应用正常运行\n"
           @"• 妥善保管您的设备和数据\n\n"
           @"3. 服务变更\n"
           @"我们保留随时修改或终止服务的权利，包括：\n"
           @"• 功能更新和改进\n"
           @"• 服务条款调整\n"
           @"• 技术维护和升级\n\n"
           @"4. 免责声明\n"
           @"• 本应用仅作为记忆辅助工具\n"
           @"• 我们不对数据丢失承担责任\n"
           @"• 建议您定期备份重要数据\n"
           @"• 请根据实际情况使用提醒功能\n\n"
           @"5. 知识产权\n"
           @"• 本应用的所有知识产权归我们所有\n"
           @"• 您的数据内容归您所有\n"
           @"• 未经许可不得复制或传播本应用\n\n"
           @"6. 联系我们\n"
           @"如有任何问题或建议，请联系我们。\n\n"
           @"感谢您的使用！\n\n"
           @"最后更新时间：2024年1月";
}

@end
